<?php

namespace App\Services;

//百融风控
use App\Models\MemberLegal;
use App\Models\Order;
use App\Models\OrderRemark;
use log\Log;
use remote\remoteProcessor;
use App\Exceptions\BaseException;


class KuaiQianPayService
{
    /**
     * 签约申请
     * @param $rawData 前端传过来的json字符串
     */
    public function readySign($arrayData){
//        Log::info('------------------------------------------------------------');
//        Log::info('A1001接口接收到前端数据：'.$arrayData);
        //开始组装报文
        $request_Head_Array = array();
        $request_Head_Array['version'] = "1.0.0";
        //---1.组装明文head数组
        $request_Head_Array['messageType'] = "A1001";
        $request_Head_Array['memberCode'] = config('kuaiqian.memberCode');
        $request_Head_Array['externalRefNumber'] = $arrayData['external_ref_number'];
        //---2.组装明文body数组
        $request_Body_Array['merchantId'] = config('kuaiqian.merchantId');
        $request_Body_Array['terminalId'] = config('kuaiqian.terminalId');
        $request_Body_Array['customerId'] = 'dlz00000'.$arrayData['uid'];
        $request_Body_Array['pan'] = $arrayData['card_no'];//银行卡号
        $request_Body_Array['cardHolderName'] = $arrayData['card_holder_name'];//姓名
//        $request_Body_Array['idType'] = 0;
        $request_Body_Array['cardHolderId'] = $arrayData['cert_no'];//证件号码
//        $request_Body_Array['expiredDate'] = $arrayData['expiredDate'];//	对应信用卡的有效期，格式为MMYY月月/年年。借记卡无需上送
//        $request_Body_Array['cvv2'] = $arrayData['cvv2'];//信用卡片背面的CVV2。借记卡无需上送
        $request_Body_Array['phoneNo'] = $arrayData['mobile_no'];//手机号
//        $request_Body_Array['bindType'] = $arrayData['bindType'];
        $request_Body_Array['deviceType'] = '02';
        $request_Body_Array['sourceIp'] = getRealIp();
        $request_Body_Array['appName'] = '都来租物';
        $request_Body_Array['protocolVersion'] = '1.0';
        $request_Body_Array['protocolNo'] =$arrayData['uid'];
//        $request_Body_Array['reMark'] = $arrayData['reMark'];

        /**
         * 请求快钱，获取返回的明文
         * 提醒注意：如涉及敏感信息，请加工处理后再给前端
         */
        $responseDecryptMessage = remoteProcessor::requestKQ($request_Head_Array,$request_Body_Array,config('kuaiqian'));
        $this->addLog('签约申请',$request_Body_Array,$responseDecryptMessage);

        if($responseDecryptMessage['bizResponseCode']!='0000'){
            throw new BaseException($responseDecryptMessage['bizResponseMessage']);
        }
        return $responseDecryptMessage['token'];
    }

    /**
     * 签约短信验证
     * @param $rawData 前端传过来的json字符串
     */
    public function confirmSign($arrayData){
        //---1.组装明文head数组
        $request_Head_Array = array();
        $request_Head_Array['version'] = "1.0.0";
        $request_Head_Array['messageType'] = "A1002";
        $request_Head_Array['memberCode'] = config('kuaiqian.memberCode');
        //注意，此处externalRefNumber须与A1001的externalRefNumber相同
        $request_Head_Array['externalRefNumber'] = $arrayData['external_ref_number'];
        //---2.组装明文body数组
        $request_Body_Array['merchantId'] =  config('kuaiqian.merchantId');
        $request_Body_Array['terminalId'] =  config('kuaiqian.terminalId');
        $request_Body_Array['customerId'] = 'dlz00000'.$arrayData['uid'];
        $request_Body_Array['pan'] = $arrayData['card_no'];
        $request_Body_Array['phoneNo'] = $arrayData['mobile_no'];
        $request_Body_Array['bindType'] = '0';
        $request_Body_Array['validCode'] = $arrayData['verificationCode'];
        $request_Body_Array['token'] = $arrayData['msgId'];
        /**
         * 请求快钱，获取返回的明文
         * 提醒注意：如涉及敏感信息，请加工处理后再给前端
         */
        $responseDecryptMessage = remoteProcessor::requestKQ($request_Head_Array,$request_Body_Array,config('kuaiqian'));
        $this->addLog('签约短信验证',$request_Body_Array,$responseDecryptMessage);

        if($responseDecryptMessage['bizResponseCode']!='0000'){
            throw new BaseException($responseDecryptMessage['bizResponseMessage']);
        }
        return $responseDecryptMessage['payToken'];
    }

    /**
     * 协议支付
     * @param $rawData 前端传过来的json字符串
     */
    public function confirmPay($arrayData){
        //开始组装报文
        //---1.组装明文head数组
        $request_Head_Array = array();
        $request_Head_Array['version'] = "1.0.0";
        $request_Head_Array['messageType'] = "A1003";
        $request_Head_Array['memberCode'] =  config('kuaiqian.memberCode');
        $request_Head_Array['externalRefNumber'] = $arrayData['trans_id'];

        //---2.组装明文body数组
        $request_Body_Array['merchantId'] =  config('kuaiqian.merchantId');
        $request_Body_Array['terminalId'] =  config('kuaiqian.terminalId');
        $request_Body_Array['settleMerchantId'] = config('kuaiqian.merchantId');
        $request_Body_Array['entryTime'] =  date('YmdHis',date());
        $request_Body_Array['customerId'] = 'dlz00000'.$arrayData['uid'];
        $request_Body_Array['amount'] = bcmul($arrayData['txn_amt'],100);
//        $request_Body_Array['ext'] = $arrayData['ext'];
//        $request_Body_Array['ext1'] = $arrayData['ext1'];
//        $request_Body_Array['storablePan'] = substr($arrayData['storablePan'],0,6).substr($arrayData['storablePan'],-4,4);
        $request_Body_Array['payToken'] = $arrayData['protocol_no'];
//        $request_Body_Array['tr3Url'] = $arrayData['tr3Url'];
//        $request_Body_Array['bankId'] = $arrayData['bankId'];
//        $request_Body_Array['bindType'] = $arrayData['bindType'];
//        $request_Body_Array['deviceType'] = $arrayData['deviceType'];
//        $request_Body_Array['deviceNo'] = $arrayData['deviceNo'];
        $request_Body_Array['sourceIp'] = getRealIp();
//        $request_Body_Array['sourceMacAddr'] = $arrayData['sourceMacAddr'];
//        $request_Body_Array['sourceImei'] = $arrayData['sourceImei'];
//        $request_Body_Array['sourceIccid'] = $arrayData['sourceIccid'];
//        $request_Body_Array['sourceWifiMacAddr'] = $arrayData['sourceWifiMacAddr'];
//        $request_Body_Array['lbs'] = $arrayData['lbs'];
//        $request_Body_Array['sourceImsi'] = $arrayData['sourceImsi'];
//        $request_Body_Array['serviceType'] = $arrayData['serviceType'];
//        $request_Body_Array['tdpformName'] = $arrayData['tdpformName'];
        $request_Body_Array['productNum'] = '1';
        $request_Body_Array['productName'] = $arrayData['orderName'];
//        $request_Body_Array['sharingInfo'] = $arrayData['sharingInfo'];
        /**
         * 请求快钱，获取返回的明文
         * 提醒注意：如涉及敏感信息，请加工处理后再给前端
         */
        $responseDecryptMessage = remoteProcessor::requestKQ($request_Head_Array,$request_Body_Array,config('kuaiqian'));
        $request_Body_Array=array_merge($request_Body_Array,$request_Head_Array);
        $this->addLog('协议支付',$request_Body_Array,$responseDecryptMessage);
        if($responseDecryptMessage['bizResponseCode']!='0000'){
            throw new BaseException($responseDecryptMessage['bizResponseMessage']);
        }


        return $responseDecryptMessage;
    }

    /**
     * 退款申请
     */
    public function refund($arrayData){

        //---1.组装明文head数组
        $request_Head_Array = array();
        $request_Head_Array['version'] = "1.0.0";
        $request_Head_Array['messageType'] = "A9007";
        $request_Head_Array['memberCode'] =  config('kuaiqian.memberCode');
        $request_Head_Array['externalRefNumber'] = $arrayData['origMerchantOrderNo'];

        //---2.组装明文body数组
        $request_Body_Array['merchantId'] =  config('kuaiqian.merchantId');
        $request_Body_Array['terminalId'] =  config('kuaiqian.terminalId');
        $request_Body_Array['origRefNumber'] = $arrayData['origMerchantOrderNo'];
//        $request_Body_Array['settleMerchantId'] = $arrayData['settleMerchantId'];
        $request_Body_Array['entryTime'] = date('YmdHis',date());
        $request_Body_Array['amount'] =$arrayData['refundAmount'];
//        $request_Body_Array['refundSharingInfo'] = $arrayData['refundSharingInfo'];

        /**
         * 请求快钱，获取返回的明文
         * 提醒注意：如涉及敏感信息，请加工处理后再给前端
         *
         */
        $responseDecryptMessage = remoteProcessor::requestKQ($request_Head_Array,$request_Body_Array,config('kuaiqian'));

        $this->addLog('协议支付退款申请',$request_Body_Array,$responseDecryptMessage);

        if($responseDecryptMessage['bizResponseCode']!='0000'){
            throw new BaseException($responseDecryptMessage['bizResponseMessage']);
        }
        return $responseDecryptMessage;

    }

    public static function addLog($message,$request=[],$response=[]){
        $content = [
            'request'=>is_object($request) ? object_array($request) : $request,
            'response'=>is_object($response) ? object_array($response) : $response,
        ];
        \Illuminate\Support\Facades\Log::debug($message,$content);
    }

}
